local CheckWidgetType = UI.CheckWidgetType
local CheckArgType = UI.CheckArgType
local CheckArgWidget = UI.CheckArgWidget
local MakeAccessor = UI.MakeAccessor
local MakeTypeSetter = UI.MakeTypeSetter
local MakeTextureFunctions = UI.MakeTextureFunctions

--[[ Button @FIXME ]]--
local Button = UI.NewWidgetType("Button", "Frame")
Button.parameters = {
	bstate = "NORMAL",
	enabled = true,
	dtcolred = 0.5,
	dtcolgreen = 0.5,
	dtcolblue = 0.5,
	dtcolalpha = 1,
	textcolorred = 1,
	textcolorgreen = 1,
	textcolorblue = 1,
	textcoloralpha = 1,
	htextcolorred = 1,
	htextcolorgreen = 1,
	htextcolorblue = 1,
	htextcoloralpha = 1,
	highlightlocked = false,
	allowmouseover = false,
}

MakeAccessor("Button", "GetAllowMouseover", "allowmouseover")
MakeTypeSetter("Button", "SetAllowMouseover", "allowmouseover", "boolean")

function Button:Initialize(o)
	local t = UI.params[o]
	assert(t)
end

function Button.prototype:Click()
	CheckWidgetType("Button:Click", self, "Button")
	local t = UI.params[self]
	t.bstate = "PUSHED"
	UI.CallScript(self, "PreClick")
	UI.CallScript(self, "OnClick")
	UI.CallScript(self, "PostClick")
	t.bstate = "NORMAL"
end

function Button.prototype:RegisterForClicks(...)
	CheckWidgetType("Button:RegisterForClicks", self, "Button")
	-- @FIXME ???
end

function Button.prototype:RegisterForDrag(...)
	CheckWidgetType("Button:RegisterForDrag", self, "Button")
	-- @FIXME ???
end

function Button.prototype:SetText(text)
	CheckWidgetType("Button:SetText", self, "Button")
	CheckArgType("Button:SetText", 2, "text", {"string", "number", "nil"}, text)
	local t = UI.params[self]
	if not t.fs then
		t.fs = self:CreateFontString(nil, "OVERLAY")
		t.fs:SetAllPoints(self)
	end
	t.fs:SetText(text and tostring(text))
end

function Button.prototype:GetText()
	CheckWidgetType("Button:GetText", self, "Button")
	local t = UI.params[self]
	if not t.fs then
		t.fs = self:CreateFontString()
		t.fs:SetAllPoints(self)
	end
	return t.fs:GetText()
end

function Button.prototype:SetFormattedText(s, ...)
	CheckWidgetType("Button:SetFormattedText", self, "Button")
	CheckArgType("Button:SetFormattedText", 2, "text", "string", s)
	self:SetText(s:format(...))
end

function Button.prototype:GetTextWidth()
	CheckWidgetType("Button:GetTextWidth", self, "Button")
	local text = self:GetText()
	if not text then return 0 end
	return text:len() * 8 -- @FIXME
end

function Button.prototype:Disable()
	CheckWidgetType("Button:Disable", self, "Button")
	UI.params[self].enabled = false
	UI.CallScript(self, "OnDisable")
end

function Button.prototype:Enable()
	CheckWidgetType("Button:Enable", self, "Button")
	UI.params[self].enabled = true
	UI.CallScript(self, "OnEnable")
end

MakeAccessor("Button", "IsEnabled", "enabled")

MakeTextureFunctions(Button, "Button", "Normal", true)
MakeTextureFunctions(Button, "Button", "Pushed")
MakeTextureFunctions(Button, "Button", "Highlight")
MakeTextureFunctions(Button, "Button", "Disabled")

function Button.prototype:SetDisabledTextColor(r, g, b, a)
	CheckWidgetType("Button:SetDisabledTextColor", self, "Button")
	local t = UI.params[self]
	t.dtcolred = r
	t.dtcolgreen = g
	t.dtcolblue = b
	t.dtcolalpha = a
end

function Button.prototype:SetFont()
	CheckWidgetType("Button:SetFont", self, "Button")
	--@FIXME
end

function Button.prototype:GetFont()
	CheckWidgetType("Button:GetFont", self, "Button")
	--@FIXME
end

function Button.prototype:SetNormalFontObject(o)
	CheckWidgetType("Button:SetNormalFontObject", self, "Button")
	--@FIXME
	local t = UI.params[self]
	if not t.fs then
		t.fs = self:CreateFontString()
		t.fs:SetAllPoints(self)
	end
	t.fs:SetFontObject(o)
end

function Button.prototype:SetHighlightFontObject()
	CheckWidgetType("Button:SetHighlightFontObject", self, "Button")
	--@FIXME
end

function Button.prototype:SetPushedTextOffset()
	CheckWidgetType("Button:SetPushedTextOffset", self, "Button")
	--@FIXME
end

function Button.prototype:SetDisabledFontObject()
	CheckWidgetType("Button:SetDisabledFontObject", self, "Button")
	--@FIXME
end

function Button.prototype:UnlockHighlight()
	CheckWidgetType("Button:UnlockHighlight", self, "Button")
	local t = UI.params[self]
	t.highlightlocked = false
end

function Button.prototype:LockHighlight()
	CheckWidgetType("Button:LockHighlight", self, "Button")
	local t = UI.params[self]
	t.highlightlocked = true
end

MakeAccessor("Button", "GetButtonState", "bstate")
MakeTypeSetter("Button", "SetButtonState", "bstate", "string")

MakeAccessor("Button", "GetTextColor", {"textcolorred", "textcolorgreen", "textcolorblue", "textcoloralpha"})
MakeTypeSetter("Button", "SetTextColor", {"textcolorred", "textcolorgreen", "textcolorblue", "textcoloralpha"}, {"number", "number", "number", {"number", "nil"}})
MakeAccessor("Button", "GetHighlightTextColor", {"htextcolorred", "htextcolorgreen", "htextcolorblue", "htextcoloralpha"})
MakeTypeSetter("Button", "SetHighlightTextColor", {"htextcolorred", "htextcolorgreen", "htextcolorblue", "htextcoloralpha"}, {"number", "number", "number", {"number", "nil"}})

function Button.prototype:SetFontString(fs)
	CheckWidgetType("Button:SetFontString", self, "Button")
	local t = UI.params[self]
	CheckArgWidget("Button:SetFontString", 2, "fs", "FontString", fs)
	t.fs = fs
end

function Button.prototype:GetFontString()
	CheckWidgetType("Button:GetFontString", self, "Button")
	return UI.params[self].fs
end

UI.RegisterScriptHandlers(Button, "OnClick", "OnDoubleClick", "PreClick", "PostClick", "OnEnable", "OnDisable")
